<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta name="description" content="">
    <meta http-equiv="x-dns-prefetch-control" content="on">

    <title>已就位第一步</title>
    <link href="../../css/css.css" rel="stylesheet" type="text/css">
    <script src="../../js/jquery-v1.11.2.min.js"></script>
    <script src="../../js/driver.js"></script>
    <script src="../../js/common.js"></script>
</head>
<body>
<section class="itemgroup mt10 p15">
    <div class="twoitem_xl">
        <span id="startAddr">
            <i class="iconfont mr5 red icon28">&#xe70d;</i>

        </span>
        <span class="mt10" id="startCross">
            <i class="iconfont mr5 gray icon28">&#xe723;</i>

        </span>
    </div>
    <div class="oneitem_xl">
        <label class="gray mr5">联系人:</label>
        <span id="consignName">方大同</span>
        <i class="iconfont rightsmallicon">&#xe6db;</i>
        <label class="right" id="consignMobile">13800138000</label>
    </div>
</section>
<section class="itemgroup mt10">
    <div class="order_title_xl">重要提示</div>
    <div class="textarea_xl">
        <p>1. 请与起始点联系人再次确认配送要求和送货地址;</p>
        <p>2.请不要退出程序，否则会影响本单奖励</p>
    </div>
</section>
<section class="bottomarea" id="btid">
    <button type="button" class="btn bottomonebtn_xl" onclick="goonBid2()">我已就位</button>
</section>
<form id="dataForm">
    <input type="hidden" name="waybillId" id="waybillId">
    <input type="hidden" id="longitude" name="lon">
    <input type="hidden" id="latitude" name="lat">
</form>
<script type="text/javascript">
    $(function () {
        //token验证
        checkDriverToken();
    });
    
    var latT = "";
    var lonT = "";
		var consignMobile = 0;
    $(document).ready(function () {
        var waybillId = GetArgsFromHref("waybillId");
        
        try{
		        	delegate.setCookie("billidcookie", waybillId+"");
		    }catch(e){}
		      		
        $("#waybillId").val(waybillId);
        var url = _global.serverHost + "/driver/goon_bid1.htm";
        var data = {waybillId: waybillId};
        _callAjaxRequest(url, data, startBidBack);

        

        function startBidBack(data) {
            $("#startAddr").html('<i class="iconfont mr5 green icon20">&#xe70d;</i>&nbsp;' + data.waybill.startStreet);
            $("#startCross").html('<i class="iconfont mr5 gray icon28">&#xe723;</i>&nbsp;' + data.waybill.startCross);
            $("#consignName").html(data.waybill.consignName);
            $("#consignMobile").html(data.waybill.consignMobile);
            consignMobile = data.waybill.consignMobile;
            
            if(consignMobile=='10000000000'){
            	$("#btid").html('<button class="btn bottomone2btn left" onclick="cancelBill('+data.waybill.waybillId+')">取消订单</button><button class="btn bottomone2btn right" onclick="goonBid2()">我已就位</button>');
            }
            
            try{
            	latT = data.waybill.startLat;
            	lonT = data.waybill.startLon;
		        	var distance = delegate.getCurrentDistance(data.waybill.startLat, data.waybill.startLon);
		    		}catch(e){}
        }

        uploadLocation(waybillId);
    });

    function goonBid2() {
    		var farAwayStart = 0;
    		try{
        	var distance = delegate.getCurrentDistance(latT, lonT);
        	if(consignMobile!='10000000000'){
	        	if(distance>1.5){
	        		//alert("当前位置和起点距离较远，请稍后再试！");
	        		var update = confirm('您现在距离起点过远，点击“确定”按钮将强行到达起点，后续客户有任何投诉将由您负责！您是否要继续？');
	        		if(update){
	        			farAwayStart = 1;
	        		}else{
	        			return false;	
	        		}
	        	}
        	}
    		}catch(e){
    			alert("当前位置未达到起点距离附近，请稍后再试！");
    		}
		    	
        var url = _global.serverHost + "/driver/check_goon_bid1.htm?farAwayStart="+farAwayStart+"&driverUuid=" + getCookie("driverUuid");
        var data = $("#dataForm").serialize();
        _callAjaxRequest(url, data, goonBid1Back);
    }

    function goonBid1Back(data) {
        if (data.code == "1") {
            window.location.href = "../start_bid/goon_bid2.html?waybillId=" + data.waybillId;
        } else {
            alert(data.message);
        }
    }
    
    function cancelBill(waybillId){
    		if(confirm("确定要取消订单吗")){
	   		$.ajax({
	   		        type:'POST',
	   		        url:_global.serverHost + '/consignerOrder/deleteOrder.htm',
	   		        data: {'waybillId':waybillId},
	   		        contentType:"application/x-www-form-urlencoded;charset=UTF-8",
	   		        success: function (data) {
	   		        	
	   		        	if(data.outResult=='1'){
	   		        		window.location.href="../order/order_list.html";
	   		        	}else{
	   		        		alert(data.outResultReason);
	   		        	}
	   		        }
	   		    });
	   		}
    }
    
    function uploadLocation(waybillId) {
        var loc = "31.26270474,121.4037323";
        if (typeof delegate != "undefined") {
            loc = delegate.getCurrentLocation();
        }
        //var locs = loc.split(",");
        var positionStr = loc.replace("=", ":").replace("=", ":");
				//string 转化为json
				var positionObj = eval('(' + positionStr + ')');

        $("#longitude").val(positionObj.lon);
        $("#latitude").val(positionObj.lat);
        var json_data = {
            waybillIds: waybillId,
            lat: positionObj.lat,
            lon: positionObj.lon,
            comment: "定时定位"
        };
        if (_global.sendPost == "0") {
            //此处我只需要拿一次司机的经纬度，用于做是否到点的判断
//            _callAjaxRequest(_global.serverHost + '/driver/location.htm', json_data, function (data) {
//                console.log(JSON.stringify(data));
//            });
        } else {
            callHTTPRequestLogon(JSON.stringify(json_data));//app调用
        }
    }
</script>
</body>
</html>